package com.qifenqian.turbot.module.adapter.restaurant

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.restaurant.RestaurantServiceEntity
import kotlinx.android.synthetic.main.item_restaurant_service.view.*

/**
 * @author H
 * @createTime 2018/12/21 11:11
 * @description
 * @see
 * @since
 */
class RestaurantServiceAdapter(data: List<RestaurantServiceEntity>?) : BaseQuickAdapter<RestaurantServiceEntity, BaseViewHolder>(R.layout.item_restaurant_service, data) {

    override fun convert(helper: BaseViewHolder, item: RestaurantServiceEntity) {
        val view = helper.itemView
        view.cb_restaurant_service.isChecked = item.isChoose == 1
        view.cb_restaurant_service.text = item.serviceName
        view.btn_restaurant_service_delete.visibility = if (item.isSystem == 0) View.VISIBLE else View.INVISIBLE

        view.cb_restaurant_service.setOnCheckedChangeListener { buttonView, isChecked ->
            item.isChoose = if (isChecked) 1 else 0
        }
        view.btn_restaurant_service_delete.setOnClickListener {
            if (onItemChildClickListener != null) {
                onItemChildClickListener!!.onDelete(helper.layoutPosition, item.id)
                notifyDataSetChanged()
            }
        }
    }

    private var onItemChildClickListener: OnItemChildClickListener? = null

    fun setOnChildClickListener(listener: OnItemChildClickListener) {
        onItemChildClickListener = listener
    }

    interface OnItemChildClickListener {
        fun onDelete(position: Int, serviceId: Int)
    }
}
